package me.wangqu.weiyouqu.web.rest.util;

import java.util.regex.Pattern;

/**
 * 用于校验数据格式
 * Created by arcticfox on 4/27/16.
 */
public class RegexUtil {

    /**
     * 验证手机号
     */
    public static final String REGEX_MOBILE = "^1(3|5|7|8|9)[0-9]{9}$";

    /**
     * 验证邮箱
     */
    public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";

    /**
     * 验证汉字 - ^[一-龥],{0,}$
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
    /**
     * [Α-￥]+$
     */
    public static final String REGEX_CHINESE_ALL = "^[\u0391-\uFFE5]+$";

    /**
     * 验证18位身份证
     */
    public static final String REGEX_ID_CARD = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$";

    /**
     * 验证URL
     */
    public static final String REGEX_URL = "^[a-zA-z]+://[^\\s]*$";


    /**
     * 判断输入的字符串是否符合手机格式.
     *
     * @param mobile
     * 				传入的字符串
     * @return 符合Phone格式返回true，否则返回false
     */
    public static boolean isMobile(String mobile) {
        if (mobile == null || mobile.length() != 11) {
            return false;
        }
        Pattern pattern = Pattern.compile(REGEX_MOBILE);
        return pattern.matcher(mobile).matches();
    }


    /**
     * 判断输入的字符串是否符合Email格式.
     *
     * @param email
     * 				传入的字符串
     * @return 符合Email格式返回true，否则返回false
     */
    public static boolean isEmail(String email) {
        if (email == null || email.length() < 1 || email.length() > 256) {
            return false;
        }
        Pattern pattern = Pattern.compile(REGEX_EMAIL);
        return pattern.matcher(email).matches();
    }

    /**
     * 判断输入的字符串是否为纯汉字
     *
     * @param value
     * 				传入的字符串
     * @return
     */
    public static boolean isChinese(String value) {
        Pattern pattern = Pattern.compile(REGEX_CHINESE_ALL);
        return pattern.matcher(value).matches();
    }

    /**
     * 判断是否为浮点数，包括double和float
     *
     * @param value
     * 			传入的字符串
     * @return
     */
    public static boolean isDouble(String value) {
        Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");
        return pattern.matcher(value).matches();
    }

    /**
     * 判断是否为整数
     *
     * @param value
     * 			传入的字符串
     * @return
     */
    public static boolean isInteger(String value) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
        return pattern.matcher(value).matches();
    }
}
